//
//  FANHomeTableViewController.m
//  Weibo - OC
//
//  Created by user on 16/4/16.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANHomeTableViewController.h"
#import "FANVisitorView.h"
#import "FANHomeCell.h"
#import "FANStatusListViewModel.h"
#import "FANRefreshControl.h"


@interface FANHomeTableViewController ()

@property(nonatomic, strong)FANStatusListViewModel *statusListViewModel;

@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;

@property (nonatomic, strong) FANRefreshControl *FRefreshControl;

@end

static NSString *identifier = @"HomeCell";

@implementation FANHomeTableViewController

- (FANRefreshControl *)FRefreshControl{
    
    if (!_FRefreshControl) {
        
        _FRefreshControl = [[FANRefreshControl alloc] init];
    }
    
    return _FRefreshControl;
}

- (UIActivityIndicatorView *)indicatorView{
    
    if (!_indicatorView) {
        
        _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        
//        _indicatorView.hidesWhenStopped = false;
    }
    
    return _indicatorView;
}

- (FANStatusListViewModel *)statusListViewModel{
    
    if (!_statusListViewModel) {
        
        _statusListViewModel = [[FANStatusListViewModel alloc] init];
    }
    
    return _statusListViewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (!self.isLogin) {
        
        [self.visitorView setLoginInfo:nil andTitle:nil];

        return;
    }
    
    [self setupUI];
    
    
    [self loadData];
    
 }

- (void)setupUI{
    
    [self.tableView registerClass:[FANHomeCell class] forCellReuseIdentifier:identifier];
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.estimatedRowHeight = 250;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.backgroundColor = [UIColor colorWithWhite:220/255.0 alpha:1];
    
    self.tableView.tableFooterView = self.indicatorView;
    
    [self.tableView addSubview:self.FRefreshControl];
    
    [self.FRefreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];

}

- (void)loadData{
    
  
    [self.statusListViewModel loadData:self.indicatorView.isAnimating and:^(BOOL isSuccess) {
       
        if (isSuccess) {
            
            
            
            [self.tableView reloadData];
            
            //            NSLog(@"%ld",self.statusListViewModel.statusViewModelArray.count);
            
            
        }else{
            
            NSLog(@"数据刷新失败");
        }
        
        [self endRefresh];

    }];
}

- (void)endRefresh{
    
    [self.indicatorView stopAnimating];
    
    [self.FRefreshControl endRefreshing];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
//    NSLog(@"%ld",self.statusListViewModel.statusViewModelArray.count);
    
    return self.statusListViewModel.statusViewModelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FANHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
//    self.statusListViewModel.statusViewModelArray[indexPath.row];
    cell.statusViewModel = self.statusListViewModel.statusViewModelArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == self.statusListViewModel.statusViewModelArray.count - 1) {
        
        [self.indicatorView startAnimating];
        
        [self loadData];
    }
}

@end
